//
//  NSObject+UserDefault.m
//  OralRobot
//
//  Created by 王桂敏 on 16/5/1.
//  Copyright © 2016年 wangguimin. All rights reserved.
//

#import "NSObject+UserDefault.h"
#import "LoginViewController.h"

#define KEY_FIRSTLANCHER @"firstLancher" // 记录用户是否第一次启动

#define KEY_FIRSTLOGIN @"firstLogin" // 记录用户是否第一次登录

#define KEY_REGISTRATIONID @"registrationID" // 极光推送设备注册id

#define KEY_USERID @"userID" // 用户id

#define KEY_PHONE @"phone" // 手机号

#define KEY_PASSWORD @"password" // 密码

#define KEY_ISLOGIN @"isLogin" // 是否登录

#define KEY_NICKNAME @"nickName" // 用户名

#define KEY_ICONURL @"iconURL" // 头像

#define KEY_ORGNAME @"orgName" // 分销商

#define KEY_ORGICONURL @"orgIconUrl"  //分销商图片

#define KEY_ORGEMAIL @"orgEmail" // 分销商邮箱

#define KEY_ORGADRESS @"orgAdress" //分销商地址

#define KEY_TOKEN @"token"  //  token

#define KEY_GROUNDTIME @"backgroundTime"  //  backgroundTime

#define KEY_BEGINTTIME @"beginTime"  //  beginTime

#define KEY_ENDTIME @"endTime"  //  endTime

#define KEY_PROVINCE @"province"  //  province

#define KEY_SHORTPROVINCE @"shortProvince"  //  shortProvince

#define KEY_CITY @"city"  //  city

#define KEY_OPENID @"openId"  //openId

#define KEY_OPENTYPEOFTHIRD @"openTypeOfThird"  //openType

#define KEY_LEGALPERSON @"legalPerson"

#define KEY_CARDNUM @"cardNum"

#define KEY_BALANCE @"balance"

#define KEY_ISSHOW @"isShow"

#define KEY_IDENTITY @"identity"

#define KEY_StuJoin @"stuJoin"

#define KEY_TeacherJoin @"teacherJoin"

#define KEY_BindStatus @"bindStatus"

#define KEY_FIRSTNETWORK @"firstNetwork"


#define KEY_UserGender @"gender"

#define KEY_Active @"active"

#define KEY_Payfor @"payfor"

#define KEY_FirstLogin @"firstLogin"

#define KEY_Sex @"sex"

#define KEY_Ignore @"ignore"


@implementation NSObject (UserDefaults)

/**
 *  app首次启动
 */
- (BOOL)firstLancher{
    return [[NSUserDefaults standardUserDefaults] boolForKey:KEY_FIRSTLANCHER];
}
- (void)setFirstLancher:(BOOL)firstLancher{
    [[NSUserDefaults standardUserDefaults] setBool:firstLancher forKey:KEY_FIRSTLANCHER];
}
/**
 *  app首次登录
 */
- (BOOL)firstLogin{
    return [[NSUserDefaults standardUserDefaults] boolForKey:KEY_FIRSTLOGIN];
}
- (void)setFirstLogin:(BOOL)firstLogin{
    [[NSUserDefaults standardUserDefaults] setBool:firstLogin forKey:KEY_FIRSTLOGIN];
}
/**
 *  极光推送设备注册id
 */
- (NSString *)registrationID{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_REGISTRATIONID] length] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_REGISTRATIONID] :  @"191e35f7e04563a4fc1";
}
- (void)setRegistrationID:(NSString *)registrationID{
    [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:KEY_REGISTRATIONID];
}

/**
 *  手机号
 */
- (NSString *)phone{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_PHONE];
}
- (void)setPhone:(NSString *)phone{
    [[NSUserDefaults standardUserDefaults] setObject:phone forKey:KEY_PHONE];
}

/**
 *  密码
 */
- (NSString *)password{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_PASSWORD];
}
- (void)setPassword:(NSString *)password{
    [[NSUserDefaults standardUserDefaults] setObject:password forKey:KEY_PASSWORD];
}

/**
 *  用户id
 */
- (NSString *)userID{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_USERID] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_USERID] : @"";
}
- (void)setUserID:(NSString *)userID{
    [[NSUserDefaults standardUserDefaults] setObject:userID forKey:KEY_USERID];
}

/**
 *  用户用户名
 */
- (NSString *)nickName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_NICKNAME] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_NICKNAME] : @"";
}
- (void)setNickName:(NSString *)nickName{
    [[NSUserDefaults standardUserDefaults] setObject:nickName forKey:KEY_NICKNAME];
}

/**
 *  头像
 */
- (NSString *)iconURL{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_ICONURL] length] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ICONURL] : @"";
}
- (void)setIconURL:(NSString *)iconURL{

    if ([iconURL description].length) {
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",iconURL] forKey:KEY_ICONURL];
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_ICONURL];
    }
    
}

/**
 *  分销商/学校
 */
- (NSString *)orgName{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGNAME] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGNAME] : @"";
}
- (void)setOrgName:(NSString *)orgName{
    
    [[NSUserDefaults standardUserDefaults] setObject:orgName forKey:KEY_ORGNAME];
}

/**
 *   分销商/学校 图片
 */
- (NSString *)orgIconUrl{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGICONURL] length] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGICONURL] : @"";
}

-(void)setOrgIconUrl:(NSString *)orgIconUrl {
    if ([orgIconUrl description].length) {
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",orgIconUrl] forKey:KEY_ORGICONURL];
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:KEY_ORGICONURL];
    }
}

/**
 *  分销商/学校 邮箱
 */
- (NSString *)orgEmail{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGEMAIL] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGEMAIL] : @"";
}
-(void)setOrgEmail:(NSString *)orgEmail {
    [[NSUserDefaults standardUserDefaults] setObject:orgEmail forKey:KEY_ORGEMAIL];
}

/**
 *  分销商/学校 地址
 */
- (NSString *)orgAdress{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGADRESS] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ORGADRESS] : @"";
}

-(void)setOrgAdress:(NSString *)orgAdress {
    [[NSUserDefaults standardUserDefaults] setObject:orgAdress forKey:KEY_ORGADRESS];
}

/**
 *  token
 *
 */
- (NSString *)token{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_TOKEN] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_TOKEN] : @"";
}
- (void)setToken:(NSString *)token{
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:KEY_TOKEN];
}

/**
 *  backgroundTime：yyyyMMddHHmmss
 */
- (NSString *)backgroundTime{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_GROUNDTIME] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_GROUNDTIME] : @"";
}

- (void)setBackgroundTime:(NSString *)backgroundTime{
    [[NSUserDefaults standardUserDefaults] setObject:backgroundTime forKey:KEY_GROUNDTIME];
}

/**
 *  beginTime：yyyyMMddHHmmss
 */
- (NSString *)beginTime{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_BEGINTTIME] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_BEGINTTIME] : @"";
}
- (void)setBeginTime:(NSString *)beginTime{
    [[NSUserDefaults standardUserDefaults] setObject:beginTime forKey:KEY_BEGINTTIME];

}


/**
 *  endTime：yyyyMMddHHmmss
 */
- (NSString *)endTime{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_GROUNDTIME] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_GROUNDTIME] : @"";
}

- (void)setEndTime:(NSString *)endTime{
    [[NSUserDefaults standardUserDefaults] setObject:endTime forKey:KEY_ENDTIME];
}

/**
 *  是否登录成功
 */
- (BOOL)isLogin{
    return [[NSUserDefaults standardUserDefaults] boolForKey:KEY_ISLOGIN];
}
- (void)setIsLogin:(BOOL)isLogin{
    [[NSUserDefaults standardUserDefaults] setBool:isLogin forKey:KEY_ISLOGIN];
}

/**
 *  province
 */
- (NSString *)province{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_PROVINCE] length] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_PROVINCE] : @"";
}

- (void)setProvince:(NSString *)province{
    [[NSUserDefaults standardUserDefaults] setObject:province forKey:KEY_PROVINCE];

}

/**
 *  shortProvince
 */
- (NSString *)shortProvince{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_SHORTPROVINCE];
}
- (void)setShortProvince:(NSString *)shortProvince{
    [[NSUserDefaults standardUserDefaults] setObject:shortProvince forKey:KEY_SHORTPROVINCE];
    
}

/**
 *  city
 */
- (NSString *)city{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_CITY] isEmpty] ? @"" : [[NSUserDefaults standardUserDefaults] objectForKey:KEY_CITY];
    
}

- (void)setCity:(NSString *)city{
    [[NSUserDefaults standardUserDefaults] setObject:city forKey:KEY_CITY];
    
}
#pragma mark - 新增加
/**
 *  企业法人
 */
-(NSString *)legalPerson {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_LEGALPERSON] isEmpty] ? @"" : [[NSUserDefaults standardUserDefaults] objectForKey:KEY_LEGALPERSON];
}

-(void)setLegalPerson:(NSString *)legalPerson {
    [[NSUserDefaults standardUserDefaults] setObject:legalPerson forKey:KEY_LEGALPERSON];
}

/**
 *  银行卡号
 */
-(NSString *)cardNum {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_CARDNUM] isEmpty] ? @"" : [[NSUserDefaults standardUserDefaults] objectForKey:KEY_CARDNUM];
}

-(void)setCardNum:(NSString *)cardNum {
    [[NSUserDefaults standardUserDefaults] setObject:cardNum forKey:KEY_CARDNUM];
}

/**
 *  账户余额
 */
-(NSString *)balance {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_BALANCE] isEmpty] ? @"" : [[NSUserDefaults standardUserDefaults] objectForKey:KEY_BALANCE];
}

-(void)setBalance:(NSString *)balance {
    [[NSUserDefaults standardUserDefaults] setObject:balance forKey:KEY_BALANCE];
}


/**
 *  三方id
 */
- (NSString *)openId{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_OPENID];
}

- (void)setOpenId:(NSString *)openId{
    [[NSUserDefaults standardUserDefaults] setObject:openId forKey:KEY_OPENID];
}


/**
 *  三方类型
 */
- (NSString *)openTypeOfThird{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_OPENTYPEOFTHIRD];
    
}

- (void)setOpenTypeOfThird:(NSString *)openTypeOfThird{
    [[NSUserDefaults standardUserDefaults] setObject:openTypeOfThird forKey:KEY_OPENTYPEOFTHIRD];
    
}

/**
 *  三方是否显示
 */
- (NSString *)isShow{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_ISSHOW] length] ? [[NSUserDefaults standardUserDefaults] objectForKey:KEY_ISSHOW] : @"1";
}
- (void)setIsShow:(NSString *)isShow{
    [[NSUserDefaults standardUserDefaults] setObject:isShow forKey:KEY_ISSHOW];
}

// 用户身份
- (NSString *)identity{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_IDENTITY];
}

- (void)setIdentity:(NSString *)identity{
    [[NSUserDefaults standardUserDefaults] setObject:identity forKey:KEY_IDENTITY];
}
-(UserType)userType
{
    if ([self.identity isEqualToString:@"3"]) {
        return UserType_Teacher;
    }else{
        return UserType_Student;
    }
}
-(void)setUserType:(UserType)userType
{
    if (userType == UserType_Teacher) {
        [[NSUserDefaults standardUserDefaults] setObject:@"3" forKey:KEY_IDENTITY];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"4" forKey:KEY_IDENTITY];
    }
}

- (void)setBindStatus:(NSString *)stastu{
    [[NSUserDefaults standardUserDefaults] setObject:stastu forKey:KEY_BindStatus];
}

- (NSString *)bindStatus
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_BindStatus];
}

- (BOOL)firstNetwork{
    return [[NSUserDefaults standardUserDefaults] boolForKey:KEY_FIRSTNETWORK];
}
- (void)setFirstNetwork:(BOOL)firstNetwork{
    [[NSUserDefaults standardUserDefaults] setBool:firstNetwork forKey:KEY_FIRSTNETWORK];
}

- (void)setGender:(NSString *)gender{
    [[NSUserDefaults standardUserDefaults] setObject:gender forKey:KEY_UserGender];
}

- (NSString *)gender
{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:KEY_UserGender] isEmpty] ? @"女" : [[NSUserDefaults standardUserDefaults] objectForKey:KEY_UserGender];;
}

- (void)setActiveString:(NSString *)activeString
{
    [[NSUserDefaults standardUserDefaults] setObject:activeString forKey:KEY_Active];
}


- (NSString *)activeString
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_Active];
}

- (void)setPayforString:(NSString *)payforString
{
    [[NSUserDefaults standardUserDefaults] setObject:payforString forKey:KEY_Payfor];
}

- (NSString *)payforString
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_Payfor];
}

- (void)setIsFirstLogin:(BOOL)isFirstLogin
{
    [[NSUserDefaults standardUserDefaults] setBool:isFirstLogin forKey:KEY_FIRSTLOGIN];
}

- (BOOL)isFirstLogin
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:KEY_FIRSTLOGIN];
}

-(void)setIsIgnore:(BOOL)isIgnore
{
    [[NSUserDefaults standardUserDefaults] setBool:isIgnore forKey:KEY_Ignore];
}

- (BOOL)isIgnore
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:KEY_Ignore];
}

- (AFNetworkReachabilityStatus)netState{
    return [[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus];
}

//  退出登录后的跳转
- (void)removeUserLoginInfo
{
    //  1.保存信息
    
    [self setIsLogin:NO];

    
    [self setPassword:@""];
    [self setOrgName:@""];
    [self setIconURL:@""];
    [self setToken:@""];
    [self setUserID:@""];
    [self setRegistrationID:@""];
    [self setGender:@""];

    
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginOut object:nil];
    //  2.跳转到登录
    LoginViewController *VC = [[LoginViewController alloc] init];
    
    WGMNavigationController *naviC = [[WGMNavigationController alloc] initWithRootViewController:VC];
    [UIApplication sharedApplication].keyWindow.rootViewController = naviC;

}

@end
